package com.bys.admin.ai.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bys.admin.ai.controller.vo.ChatModelPageReqVO;
import com.bys.admin.ai.controller.vo.ChatModelSaveReqVO;
import com.bys.admin.ai.entity.ChatModel;
import com.bys.admin.ai.mapper.ChatModelMapper;
import com.bys.admin.ai.service.ChatModelService;
import com.bys.exception.ServiceException;
import com.github.yulichang.base.MPJBaseServiceImpl;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import com.bys.util.BeanUtils;

import static com.bys.constant.ErrorCodeConstant.CHAT_MODEL_NOT_EXISTS;


/**
 * ai 聊天模型 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class ChatModelServiceImpl  extends MPJBaseServiceImpl<ChatModelMapper,ChatModel> implements ChatModelService {

    @Resource
    private ChatModelMapper chatModelMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createChatModel(ChatModelSaveReqVO createReqVO) {
        // 插入
        ChatModel chatModel = BeanUtils.toBean(createReqVO, ChatModel.class);
        chatModelMapper.insert(chatModel);
        // 返回
        return chatModel.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateChatModel(ChatModelSaveReqVO updateReqVO) {
        // 校验存在
        validateChatModelExists(updateReqVO.getId());
        // 更新
        ChatModel updateObj = BeanUtils.toBean(updateReqVO, ChatModel.class);
        chatModelMapper.updateById(updateObj);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteChatModel(Long id) {
        // 校验存在
        validateChatModelExists(id);
        // 删除
        chatModelMapper.deleteById(id);
    }

    private void validateChatModelExists(Long id) {
        if (chatModelMapper.selectById(id) == null) {
            throw new ServiceException(CHAT_MODEL_NOT_EXISTS);
        }
    }
    @Override
    public ChatModel getChatModel(Long id) {
        return chatModelMapper.selectById(id);
    }
    @Override
    public Page<ChatModel> getChatModelPage(ChatModelPageReqVO pageReqVO) {
    return chatModelMapper.selectPage(pageReqVO);
    }
}